package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompMchntBlackListMapper;
import cc.rengu.igas.bomp.common.entity.MchntBlackListInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhangxuran
 * @Date: 2020/4/24 18:10
 * @Description: 黑名单查询
 */
public class BompMchntBlackListMapperImpl implements BompMchntBlackListMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<MchntBlackListInfo> selectBompMchntBlackListInfoByInstIdMchntNo(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBlackListInfoByInstIdMchntNo");

        MchntBlackListInfo mchntBlackListInfo = new MchntBlackListInfo();
        mchntBlackListInfo.setInstId(instId);
        mchntBlackListInfo.setMchntNo(mchntNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBlackListInfo, MchntBlackListInfo.class);
        if (CollectionUtils.isEmpty(objects)) {
            return null;
        }

        return objects.stream().map(item -> (MchntBlackListInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<MchntBlackListInfo> selectBompMchntBlackListInfoByMchntNetworkDomain(String mchntNetworkDomain) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBlackListInfoByMchntNetworkDomain");

        MchntBlackListInfo mchntBlackListInfo = new MchntBlackListInfo();
        String url = "%" + mchntNetworkDomain + "%";
        mchntBlackListInfo.setUrl(mchntNetworkDomain);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBlackListInfo, MchntBlackListInfo.class);
        if (CollectionUtils.isEmpty(objects)) {
            return null;
        }

        return objects.stream().map(item -> (MchntBlackListInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBlackListInfo> selectBompMchntBlackListInfoByArtifCertNo(String artifCertType, String artifCertNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBlackListInfoByArtifCertNo");

        MchntBlackListInfo mchntBlackListInfo = new MchntBlackListInfo();
        mchntBlackListInfo.setArtifCertNo(artifCertNo);
        mchntBlackListInfo.setArtifCertType(artifCertType);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBlackListInfo, MchntBlackListInfo.class);
        if (CollectionUtils.isEmpty(objects)) {
            return null;
        }

        return objects.stream().map(item -> (MchntBlackListInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBlackListInfo> selectBompMchntBlackListInfoByLicenceCode(String LicenceCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBlackListInfoByLicenceCode");

        MchntBlackListInfo mchntBlackListInfo = new MchntBlackListInfo();
        mchntBlackListInfo.setLicenceCode(LicenceCode);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBlackListInfo, MchntBlackListInfo.class);
        if (CollectionUtils.isEmpty(objects)) {
            return null;
        }

        return objects.stream().map(item -> (MchntBlackListInfo) item).collect(Collectors.toList());
    }
}
